package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.cms.service.SkuService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SkuHasUseException;
import com.junzhiit.nengji.cms.web.exception.SkuNameIsExistException;
import com.junzhiit.nengji.cms.web.exception.SkuNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SkuAddVO;
import com.junzhiit.nengji.cms.web.vo.SkuEditVO;
import com.junzhiit.nengji.cms.web.vo.SkuListVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SkuDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.Sku;

@Service
public class SkuServiceImpl extends BaseServiceImpl implements SkuService {

	@Autowired
	private SkuDAO skuDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, SkuListVO skuListVO) {

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(skuListVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		Sku sku = new Sku();
		BeanUtils.copyProperties(skuListVO, sku);
		sku.setShopId(shop.getId());

		List<Sku> list = skuDAO.findAll(sku, dtp.getStart(), dtp.getLength());
		int totalCount = skuDAO.getTotalCount(sku);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Transactional
	@Override
	public ResultVO add(SkuAddVO skuAddVO) {
		ResultVO result = new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(skuAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		Sku sku = new Sku();
		sku.setName(skuAddVO.getName());
		sku.setShopId(shop.getId());

		int count = skuDAO.queryNameIsExit(shop.getId(), sku.getName());
		if (count > 0) {
			throw new SkuNameIsExistException("sku分类名字已经存在");
		}

		Sku data = skuDAO.insert(sku);
		result.setData(data);
		return result;
	}

	@Transactional
	@Override
	public ResultVO update(SkuEditVO skuEditVO) {
		ResultVO result = new ResultVO();
		Sku sku = skuDAO.query(skuEditVO.getId());
		if (sku == null) {
			throw new SkuNotFoundException("sku分类未找到");
		}
		
		if(sku.getName().equals(skuEditVO.getName())){
			return result;
		}

		int count = skuDAO.queryNameIsExit(sku.getShopId(), skuEditVO.getName());
		if (count > 0) {
			throw new SkuNameIsExistException("sku分类名字已经存在");
		}
		sku.setName(skuEditVO.getName());
		boolean state = skuDAO.update(sku);
		result.setData(state);
		
		return result;

	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		Sku sku = skuDAO.query(id);
		if (sku == null) {
			throw new SkuNotFoundException("sku分类未找到");
		}
		int count=skuDAO.queryGoodsAboutSkuCount(id);
		if(count>0){
			throw new SkuHasUseException("sku正在被使用");
		}
		
		boolean data = skuDAO.updateToDelete(id);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO queryGoodsAboutSkuCount(String skuId){
		ResultVO result=new ResultVO();
		int data=skuDAO.queryGoodsAboutSkuCount(skuId);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO batchDelete(String[] ids) {
		ResultVO result = new ResultVO();
		boolean data = skuDAO.batchUpdateToDelete(ids);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO getAll(String sessionId){
		ResultVO result=new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		List<Sku> data=skuDAO.queryAllSku(shop.getId());
		result.setData(data);
		return result;
	}

}
